- Notifications
You must be signed in to change notification settings - Fork 842
/
Copy pathAdapterWithErrorHandler.cs
50 lines (44 loc) · 2.27 KB
/
AdapterWithErrorHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
usingSystem;
usingMicrosoft.Bot.Builder;
usingMicrosoft.Bot.Builder.Integration.AspNet.Core;
usingMicrosoft.Bot.Builder.Teams;
usingMicrosoft.Bot.Builder.TraceExtensions;
usingMicrosoft.Bot.Connector.Authentication;
usingMicrosoft.Extensions.Configuration;
usingMicrosoft.Extensions.Logging;
namespaceMicrosoft.BotBuilderSamples
{
publicclassAdapterWithErrorHandler:CloudAdapter
{
publicAdapterWithErrorHandler(IConfigurationconfiguration,IStoragestorage,BotFrameworkAuthenticationauth,ILogger<IBotFrameworkHttpAdapter>logger,ConversationStateconversationState=null)
:base(auth,logger)
{
base.Use(newTeamsSSOTokenExchangeMiddleware(storage,configuration["ConnectionName"]));
OnTurnError=async(turnContext,exception)=>
{
// Log any leaked exception from the application.
logger.LogError(exception,$"[OnTurnError] unhandled error : {exception.Message}");
// Uncomment below commented line for local debugging.
// await turnContext.SendActivityAsync($"Sorry, it looks like something went wrong. Exception Caught: {exception.Message}");
if(conversationState!=null)
{
try
{
// Delete the conversationState for the current conversation to prevent the
// bot from getting stuck in a error-loop caused by being in a bad state.
// ConversationState should be thought of as similar to "cookie-state" in a Web pages.
awaitconversationState.DeleteAsync(turnContext);
}
catch(Exceptione)
{
logger.LogError(e,$"Exception caught on attempting to Delete ConversationState : {e.Message}");
}
}
// Send a trace activity, which will be displayed in the Bot Framework Emulator
awaitturnContext.TraceActivityAsync("OnTurnError Trace",exception.Message,"https://www.botframework.com/schemas/error","TurnError");
};
}
}
}